//
//  HYMiddleViewController.m
//  HYSildeslipMenu
//
//  Created by 上官惠阳 on 2016/12/21.
//  Copyright © 2016年 上官惠阳. All rights reserved.
//

#import "HYMiddleViewController.h"
#import "HYVerCustomSlideViewController.h"
#import "HYTopViewController.h"
#import "HYButtomViewController.h"
//#import "LVCameraViewController.h"
#import "LVLeveWorldViewController.h"
//#import "NTESNavigationHandler.h"
//#import "NTESSettingViewController.h"
#import "LVImpUserViewController.h"
#import "HYBaseNavigationViewController.h"
#import "LVThreeBtnTool.h"

@interface HYMiddleViewController ()<HYVerCustomSlideViewControllerDelegate,HYVerCustomSlideViewControllerDataSource>
@property (nonatomic, weak) HYVerCustomSlideViewController *verSlideViewController;

@property (strong,nonatomic) UINavigationController *topNav;
@property (strong,nonatomic) UINavigationController *middleNav;
@property (strong,nonatomic) UINavigationController *buttomNav;
@end

@implementation HYMiddleViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUpVerViews];

    //底部三个按钮的隐藏显示监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(OnOrOffScrollMiddleView:) name:@"NotificationOnOrOffScrollMiddleView" object:nil];
}
#pragma mark 监听方法
- (void)OnOrOffScrollMiddleView:(NSNotification *)noti
{
    /*是一个数组，第一个元素是代表是否可滚动,是否显示底部拍照按钮，
     第二个元素代表是哪一个控制器发来的通知 0-上面界面,1-拍照界面,2-下面界面
     */

    NSArray *arr = [noti object];
    if (arr.count != 2) {
        return;
    }

    int indexfromVc = [arr[1] intValue];
    if (indexfromVc != self.verSlideViewController.seletedIndex) {
        return;
    }

    BOOL canScroll = [arr[0] boolValue];
    self.verSlideViewController.canScroll = canScroll;

    if(self.verSlideViewController.seletedIndex == 1){
        return;
    }

    //这个是因为顶部视图控制器不需要显示底部按钮了
    if(self.verSlideViewController.seletedIndex == 0){
        return;
    }

    [[LVThreeBtnTool shareThreeBtn] hideMiddleBtn:!canScroll];
}
#pragma mark makeUI
- (void)setUpVerViews {
    [self.verSlideViewController reloadData];

    self.verSlideViewController.seletedIndex = 2;

    self.verSlideViewController.seletedIndex = 1;
}
#pragma mark 当前显示的是哪个视图的导航
- (UINavigationController *)seletedNav
{
    if (!_seletedNav) {
        _seletedNav = self.middleNav;
    }
    return _seletedNav;
}
#pragma mark - HYVerCustomSlideViewControllerDataSource
- (UIViewController *)verSlideViewController:(HYVerCustomSlideViewController *)slideViewController viewControllerAtIndex:(NSInteger)index {

    switch (index) {
        case 0:
        {
            LVImpUserViewController *settingVc = [[LVImpUserViewController alloc] init];
            settingVc.closeBlock = ^(id obj){
                self.verSlideVc.seletedIndex = 1;
            };
            UINavigationController *nav = [self navWithRootVc:settingVc];
            self.topNav = nav;
            return nav;
        }
            break;
        case 1:
        {
            //第一期默认先到乐无世界
//            LVCameraViewController *centerVc = [[LVCameraViewController alloc] init];
            LVLeveWorldViewController *centerVc = [[LVLeveWorldViewController alloc] init];
            centerVc.ghostPressedBlock = ^(id obj){
                self.verSlideVc.seletedIndex = 0;
            };
            UINavigationController *nav = [self navWithRootVc:centerVc];
            self.middleNav = nav;
            return nav;
            return centerVc;
        }
            break;
        case 2:
        {
            HYButtomViewController *buttomVc = [[HYButtomViewController alloc] init];
            UINavigationController *nav = [self navWithRootVc:buttomVc];
            self.buttomNav = nav;
            return nav;
        }
            break;
        default:
            break;
    }
    return nil;
}

- (NSInteger)numberOfChildViewControllersInVerSlideViewController:(HYVerCustomSlideViewController *)slideViewController {
    return 3;
}
#pragma mark HYVerCustomSlideViewControllerDelegate
- (void)verCustomSlideViewController:(HYVerCustomSlideViewController *)slideViewController slideOffset:(CGPoint)slideOffset
{
    //当屏幕滑过一半就要做处理了，避免出现闪动效果
    if (slideOffset.y < kScreenHeight / 2) {
        if (_scrollBlock) {
            _scrollBlock(NO,_seletedNav);
        }
    }else if (slideOffset.y > kScreenHeight + kScreenHeight / 2){
        if (_scrollBlock) {
            _scrollBlock(NO,_seletedNav);
        }
    }else{
        if (_scrollBlock) {
            _scrollBlock(YES,_seletedNav);
        }
    }
}
- (void)verCustomSlideViewController:(HYVerCustomSlideViewController *)slideViewController slideIndex:(NSInteger)slideIndex {

    switch (slideIndex) {
        case 0:
        {
            self.seletedNav = self.topNav;
            if (_scrollBlock) {
                _scrollBlock(NO,_seletedNav);
            }

            [[LVThreeBtnTool shareThreeBtn] hideAllBtn:YES];
        }
            break;
        case 1:
        {
            self.seletedNav = self.middleNav;
            if (_scrollBlock) {
                _scrollBlock(YES,_seletedNav);
            }

            [[LVThreeBtnTool shareThreeBtn] showInTheMiddle];
            [[LVThreeBtnTool shareThreeBtn] showOtherViewUnderDirection:LVThreeBtnCenter distance:30];
        }
            break;
        case 2:
        {
            self.seletedNav = self.buttomNav;
            if (_scrollBlock) {
                _scrollBlock(NO,_seletedNav);
            }

            [[LVThreeBtnTool shareThreeBtn] showOnlyMiddle];
        }
            break;
        default:
            break;
    }
}
#pragma mark 创建滚动视图控制器
- (HYVerCustomSlideViewController *)verSlideViewController {
    if (!_verSlideViewController) {
        HYVerCustomSlideViewController *slide = [[HYVerCustomSlideViewController alloc] init];
        [self addChildVc:slide];
        _verSlideViewController = slide;
        slide.delgate = self;
        slide.dataSource = self;
    }
    _verSlideVc = _verSlideViewController;
    return _verSlideViewController;
}
#pragma mark other
- (void)addChildVc:(UIViewController *)childVc {
    if ([childVc isKindOfClass:[UIViewController class]] == NO) {
        return ;
    }
    [childVc willMoveToParentViewController:self];
    [self addChildViewController:childVc];
    [self.view addSubview:childVc.view];
    childVc.view.frame = self.view.bounds;
}
//带根视图的导航栏
- (UINavigationController *)navWithRootVc:(UIViewController *)vc
{
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
//    NTESNavigationHandler *handler = [[NTESNavigationHandler alloc] initWithNavigationController:nav];
//    nav.delegate = handler;
    HYBaseNavigationViewController *nav = [[HYBaseNavigationViewController alloc] initWithRootViewController:vc];
    return nav;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
